#-*-coding:utf8;-*-
import socket, re, random, string, time

def toupiao():
    #定义send数据
    request = '''GET /vote/web/wap/default/detail?aid=556&apply_id=87161 HTTP/1.1
Host: h5.cc.dacihua.cn
Proxy-Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
X-Forwarded-For: %(ip)s
User-Agent: %(ua)s
Accept-Language: zh-CN,en-US;q=0.8
Cookie: qianfan_deviceid=%(did)s


'''
    
    #分配IP地址
    f_ip=open('/storage/emulated/0/com.hipipal.qpyplus/scripts/ipAddr.txt', 'r')
    l_ip=f_ip.readlines()
    ipAddr = l_ip[random.randint(0, len(l_ip)-1)].strip('\n')
    print "IP地址： " + ipAddr
    
    #分配user-agent
    f_ua=open('/storage/emulated/0/com.hipipal.qpyplus/scripts/ua.txt', 'r')
    l_ua=f_ua.readlines()
    userAgent = l_ua[random.randint(0, len(l_ua)-1)].strip('\n')    
    print "UserAgent： " + userAgent 
    
    #获得设备号
    deviceId = "a00000" + string.join(random.sample('zyxwvutsrqponmlkjihgfedcba0987654321',8)).replace(' ','')
    
    #修改request
    request = request % dict(ip = ipAddr,
                             ua = userAgent,
                             did = deviceId)
    
    #建立通道
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #指明通信类型和协议家族
    s_port = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #测试端口
    
    #绑定本地端口
    port = random.randint(10000,65535)
    while(s_port.connect_ex(("",port)) == 0):
        port = random.randint(10000,65535)
    s.bind(("", port))
    s.connect(("h5.cc.dacihua.cn", 80))
    
    #print "Connected from", s.getsockname()  #获得本地机器的ip和端口号（端口号由操作系统随机分配，每次都不一样）
    #print "Connected to", s.getpeername()    #获得连接机器的ip和端口号
    
    s.send(request)
    rs = s.recv(4096)
    
    #print rs
    #print m_msg[0].decode('raw_unicode_escape')
    s.close()

sum = 0
while(True):
    sum = sum + 1
    print "第 %d 个浏览 "%sum
    toupiao()
    sleepTime = random.randint(1, 1)
    print "还需再等待：%d 秒"%sleepTime
    print "==================================="
    time.sleep(sleepTime)